package com.itheima.common;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author ChenJunJie
 * @date 2022/6/18 19:29
 * 短信验证码发送
 */
@Component
public class Sample {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public static void myClient (String phone) throws Exception {
        Config config= new Config()
                .setAccessKeyId("LTAI5tHNZ7C2S1vjid1CGknF")
                .setAccessKeySecret("xbhWXqNCg71PjDUPOYxTwL0QORVOl4");
        config.endpoint= "dysmsapi.aliyuncs.com";

        Client client = new Client(config);

        Random random = new Random();
        int i = random.nextInt(9000) + 1000;

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("瑞吉点餐")
                .setTemplateCode("SMS_243580715")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\""+i+"\"}");


        RuntimeOptions runtime = new RuntimeOptions();
        try {
            client.sendSmsWithOptions(sendSmsRequest, runtime);
            //将手机号与验证码存入token中
            // redisTemplate.opsForValue().set(phone,i);
        } catch (TeaException error) {
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception e) {
            TeaException error = new TeaException(e.getMessage(), e);
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}
